package net.zjitc.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Author admin
 * @Date 2019/4/24 9:05
 * 更新操作
 */
public class UpdateTest {
    public static void main(String[] args) throws SQLException {
        /*1、注册驱动
         * 2、获得数据库连接
         * 3、获得能操作sql语句的对象
         * 4、执行sql语句*/
        Connection connection = null;
        Statement statement = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/lgy";
            String user = "root";
            String password = "123456";
            connection = DriverManager.getConnection(url, user, password);
            statement = connection.createStatement();
            String sql = "UPDATE USER SET age=22 WHERE NAME='张三'";
            int s = statement.executeUpdate(sql);
            System.out.println("影响了" + s + "行");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
